/*
 * 22/10/2011 - 22:53
 */
package org.nettyflux.jsonrpc;

/**
 * @author Victor Williams Stafusa da Silva
 */
public class CommandReply {
    private String objectName;
    private String operation;
    private boolean success;
    private Object result;
    private String commandId;

    public CommandReply() {}

    public static CommandReply createFor(Command command, Object result) {
        CommandReply reply = new CommandReply();
        reply.setCommandId(command.getCommandId());
        reply.setObjectName(command.getObjectName());
        reply.setOperation(command.getOperation());
        reply.setSuccess(true);
        reply.setResult(result);
        return reply;
    }

    public static CommandReply createForException(Command command, Throwable failure) {
        CommandReply reply = new CommandReply();
        reply.setCommandId(command.getCommandId());
        reply.setObjectName(command.getObjectName());
        reply.setOperation(command.getOperation());
        reply.setSuccess(false);
        reply.setResult(failure);
        return reply;
    }

    public String getObjectName() {
        return objectName;
    }

    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getCommandId() {
        return commandId;
    }

    public void setCommandId(String commandId) {
        this.commandId = commandId;
    }

    public boolean isCompatibleWith(Command c) {
        return c.getObjectName().equals(this.getObjectName())
                && c.getOperation().equals(this.getOperation())
                && c.getCommandId().equals(this.getCommandId());
    }
}
